<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>注册页面</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="/client/css/style.css">
    <style>
        html,body{
            background: linear-gradient(180deg,#369,#fff);
            height: 100vh;
            width: 100vw;
            overflow: hidden;
        }
    </style>
</head>

<body class="vh-100 d-flex flex-column justify-content-center align-items-center bg-secondary">
<div class="card w-25">
    <div class="card-header">
        <h3 class="card-title">欢迎注册</h3>
    </div>
    <div class="card-body">
        <form onsubmit="return false" class="was-validated">
            <div class="form-row mb-3">
                <label for="username" class="col-2 text-justify col-form-label text-right">账号：</label>
                <input type="text" class="form-control col-7" id="username" name="username" required
                       placeholder="请填写账号">
                <p class="invalid-feedback col-3 col-form-label">请填写账号</p>
            </div>
            <div class="form-row mb-3">
                <label for="password" class="col-2 text-justify col-form-label text-right">密码：</label>
                <input type="password" class="form-control col-7" id="password" name="password" required
                       placeholder="请填写密码">
                <p class="invalid-feedback col-3 col-form-label">请填写密码</p>
            </div>
            <div class="form-row mb-3">
                <input id="submit" type="submit" class="btn btn-primary w-100  col-7 offset-2" value="注册">
            </div>
        </form>
    </div>
    <div class="small text-center card-footer">
        已经有账户?
         <a href="/login" class=" card-link"
                  style="color:deepskyblue;margin-left:10px;">点击去登录</a>
    </div>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.0/jquery.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/js/bootstrap.min.js"></script>
<script>
    $(function(){
      //点击去注册
      $("#submit").click(function (){
        let username = $("#username").val();
        let password = $("#password").val();
        if(!username || !password){
          alert("用户名或密码不能为空");
          return false;
        }
        $.ajax({
          url:"http://localhost:5000/register",
          method:"post",
          data:{
            username,password
          },
          success:function(res){
            alert(res.message);
            if(res.code === 0){
              //就说明注册成功,直接去登录页
              location.href = "http://localhost:5000/login";
            }else{
              //就说明注册失败,重新注册
              location.reload();
            }
          },
          error:function(err){
            console.log(err);
          }
        });
      });
    });
</script>
</body>

</html>